package org.jeecg.modules.flow.record.service.impl;

import org.jeecg.modules.flow.record.entity.WmsFlowMain;
import org.jeecg.modules.flow.record.entity.WmsFlowRecord;
import org.jeecg.modules.flow.record.mapper.WmsFlowRecordMapper;
import org.jeecg.modules.flow.record.mapper.WmsFlowMainMapper;
import org.jeecg.modules.flow.record.service.IWmsFlowMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 流程状态信息
 * @Author: jeecg-boot
 * @Date:   2023-09-21
 * @Version: V1.0
 */
@Service
public class WmsFlowMainServiceImpl extends ServiceImpl<WmsFlowMainMapper, WmsFlowMain> implements IWmsFlowMainService {

	@Autowired
	private WmsFlowMainMapper wmsFlowMainMapper;
	@Autowired
	private WmsFlowRecordMapper wmsFlowRecordMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(WmsFlowMain wmsFlowMain, List<WmsFlowRecord> wmsFlowRecordList) {
		wmsFlowMainMapper.insert(wmsFlowMain);
		if(wmsFlowRecordList!=null && wmsFlowRecordList.size()>0) {
			for(WmsFlowRecord entity:wmsFlowRecordList) {
				//外键设置
				entity.setFlowId(wmsFlowMain.getId());
				wmsFlowRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(WmsFlowMain wmsFlowMain,List<WmsFlowRecord> wmsFlowRecordList) {
		wmsFlowMainMapper.updateById(wmsFlowMain);
		
		//1.先删除子表数据
		wmsFlowRecordMapper.deleteByMainId(wmsFlowMain.getId());
		
		//2.子表数据重新插入
		if(wmsFlowRecordList!=null && wmsFlowRecordList.size()>0) {
			for(WmsFlowRecord entity:wmsFlowRecordList) {
				//外键设置
				entity.setFlowId(wmsFlowMain.getId());
				wmsFlowRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		wmsFlowRecordMapper.deleteByMainId(id);
		wmsFlowMainMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			wmsFlowRecordMapper.deleteByMainId(id.toString());
			wmsFlowMainMapper.deleteById(id);
		}
	}
	
}
